#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################


rm(list=ls())
rm(.Random.seed)

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")

R_STATIC_DIR <- file.path(R_DATA_PATH, "Bylot/Fixe")
R_MOBILE_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")


#'#
#'# Chargement des dependances
#'# 

#source(file.path(R_WORKING_DIRECTORY, "Utils/initCluster.R"))
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))


#'#
#'# Chargement des donnees
#'# 

locsclasses <- c(3, 2, 1, 0, "A", "B")

#staticErrors <- loadData(R_STATIC_DIR, locsclasses=locsclasses)
staticErrors <- loadErrorsFile(file.path(R_DATA_PATH, "Bylot", "static_errors.txt"))
mobileErrors <- loadErrorsFile(file.path(R_DATA_PATH, "Bylot", "mobile_errors.txt"))


getNlocsbyHour <- function (data) {
  tmp <- data[, hour := format(time, "%H")]
  tmp <- data[, day := format(time, "%d")]
  
  res <- data[, .N, by=list(day, hour, id)]
  return (res[, list(mean=mean(N), sd=sd(N))])
}

getNlocsbyDay <- function (data) {
    tmp <- data[, hour := format(time, "%H")]
    tmp <- data[, day := format(time, "%d")]
    
    res <- data[, .N, by=list(day, id)]
    return (res[, list(mean=mean(N), sd=sd(N))])
}

getNlocsbyDepl <- function (data) {
    res <- data[, .N, by=list(deployment, id)]
    return (res[, list(mean=mean(N), sd=sd(N))])
}



rs <- getNlocsbyHour(staticErrors)
rm <- getNlocsbyHour(mobileErrors)

rds <- getNlocsbyDay(staticErrors)
rdm <- getNlocsbyDay(mobileErrors)

rdeplm <- getNlocsbyDepl(mobileErrors)